package com.xebest.util.seq;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Calendar;
import java.util.Random;

/**
 * 仓储中心序列号生成规则
 *
 * @author 李贝贝
 */
@Component
public class StoreHouseSeqUtil {

    public static StoreHouseSeqUtil seqUtils = null;

    @Autowired
    protected RedisTemplate<String, String> redisTemplate;

    @PostConstruct
    public void init() {
        seqUtils = this;
    }

    /**
     * 根据seqName 查询自增长值 每次增长为1
     *
     * @param seqName  seqName
     * @param startNum 起始值 只有第一次生效
     */
    public String getNextSeq(String seqName, String startNum) {

        ValueOperations<String, String> redis = redisTemplate.opsForValue();
        Boolean isOk = redisTemplate.hasKey(seqName);

        if (isOk) {
            Long val = redis.increment(seqName, 1);
            return val.toString();
        } else {
            if (StringUtils.isBlank(startNum)) {
                startNum = "1";
            }
            redis.set(seqName, startNum);
            return startNum;
        }
    }

    /**
     * 获取或者重新初始化seq
     *
     * @author 邓国磊
     */
    private String getOrSetKeyNextSeq(String seqName, String startNum) {
        ValueOperations<String, String> redis = redisTemplate.opsForValue();
        Long val = null;
        Boolean isOk = redisTemplate.hasKey(seqName);
        if (StringUtils.isNotBlank(startNum)) {
            redis.set(seqName, startNum);
            return startNum;
        } else if (!isOk) {
            if (StringUtils.isBlank(startNum)) {
                startNum = "1";
            }
            redis.set(seqName, startNum);
            return startNum;
        } else {
            val = redis.increment(seqName, 1);
            return val.toString();
        }
    }


    private String getDateStr() {
        Calendar now = Calendar.getInstance();
        //获得6为日期
        String y = String.valueOf(now.get(Calendar.YEAR)).substring(2, 4);
        String m = String.valueOf(now.get(Calendar.MONTH) + 1);
        if (m.length() < 2) {
            m = "0" + m;
        }
        String d = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
        if (d.length() < 2) {
            d = "0" + d;
        }
        return y + m + d;
    }

    private String getLastNum(String seqName) {
        String lastNum;
        String seqNumStr = getOrSetKeyNextSeq(seqName, null);
        int seqNum = Integer.parseInt(seqNumStr);
        //当自增数大于9999重置为1开始
        if (seqNum > 9999) {
            seqNumStr = getOrSetKeyNextSeq(seqName, "1");
        }
        //计算当前自增数量需要补全的前缀字符
        int len = 4 - seqNumStr.length();
        //补全前缀
        StringBuilder prefixStr = new StringBuilder("");
        if (len > 0) {
            for (int i = 0; i < len; i++) {
                prefixStr.append("0");
            }
        }
        //格式化
        lastNum = prefixStr.toString() + seqNumStr;
        return lastNum;
    }

    private int getRandom(int min, int max) {
        Random random = new Random();
        int randNumber = random.nextInt(max - min) + min;
        return randNumber;
    }

    /**
     * 仓库信息序列号生成
     * 生成规则：商户简码  + 4增长数
     *
     * @author 李贝贝
     */
    public String createStoreIdNo(String prefixStr) {
        StringBuilder storeId = new StringBuilder(prefixStr);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STORE_INFO_STOREID);
        storeId.append(lastStr);
        return storeId.toString();
    }

    /**
     * 商户库存商品信息 库存ID序列号标识
     * 生成规则：170616 + 4位随机数 + 4增长数
     *
     * @author 李贝贝
     */
    public String createStoreinvIdNo() {
        StringBuilder storeId = new StringBuilder();
        storeId.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        storeId.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STORE_INVENTORY_STOREINVID);
        storeId.append(lastStr);
        return storeId.toString();
    }

    /**
     * 商品入库单  入库单号序列号生成
     * 生成规则：RK+6位年月日+4位随机数+4位流水号
     *
     * @author wangyp
     */
    public String createReceiptOrder() {
        StringBuilder purchOrder = new StringBuilder("RK");
        //6位年月日
        purchOrder.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        purchOrder.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STORERECEIPT_RECEIPT_ORDER);
        purchOrder.append(lastStr);
        return purchOrder.toString();
    }

    /**
     * 商品出库单  出库单号序列号生成
     * 生成规则：CK+6位年月日+4位随机数+4位流水号
     *
     * @author wangyp
     */
    public String createDeliveryOrder() {
        StringBuilder deliveryOrder = new StringBuilder("CK");
        //6位年月日
        deliveryOrder.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        deliveryOrder.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STOREDELIVERY_DELIVERY_ORDER);
        deliveryOrder.append(lastStr);
        return deliveryOrder.toString();
    }

    /**
     * 入库时产生的入库批次号
     * 生成规则： RN+6位年月日+4位随机数+4位流水号
     *
     * @author 杜春静
     */
    public String createReceiptBatchNo() {
        StringBuilder receiptBatchNo = new StringBuilder("RN");
        //6位年月日
        receiptBatchNo.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        receiptBatchNo.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STORERECEIPT_RECEIPT_BATCH);
        receiptBatchNo.append(lastStr);
        return receiptBatchNo.toString();
    }

    /**
     * 商品售后信息  售后单号序列号生成
     * 生成规则：SHD+6位年月日+4位随机数+4位流水号
     *
     * @author wangyp
     */
    public String createRejectCode() {
        StringBuilder rejectCode = new StringBuilder("TH");
        //6位年月日
        rejectCode.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        rejectCode.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_GOODSREJECT_REJECY_CODE);
        rejectCode.append(lastStr);
        return rejectCode.toString();
    }

    /**
     * 盘点单信息  盘点单号序列号生成
     * 生成规则：PD+6位年月日+4位随机数+4位流水号
     *
     * @author wangyp
     */
    public String createCheckCode() {
        StringBuilder checkCode = new StringBuilder("PD");
        //6位年月日
        checkCode.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        checkCode.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STORECHECK_CHECK_CODE);
        checkCode.append(lastStr);
        return checkCode.toString();
    }


    /**
     * 调拨单信息  调拨单单号序列号生成
     * 生成规则：DB+6位年月日+4位随机数+4位流水号
     *
     * @author wangyp
     */
    public String createAllortCode() {
        StringBuilder allotCode = new StringBuilder("DB");
        //6位年月日
        allotCode.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        allotCode.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STOREALLOT_ALLOT_CODE);
        allotCode.append(lastStr);
        return allotCode.toString();
    }

    /**
     * 询价单信息  询价单单号序列号生成
     * 生成规则：XJ+6位年月日+4位随机数+4位流水号
     *
     * @author wangyp
     */
    public String createInquiryCode() {
        StringBuilder inquirytCode = new StringBuilder("XJ");
        //6位年月日
        inquirytCode.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        inquirytCode.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STOREALLOT_ALLOT_CODE);
        inquirytCode.append(lastStr);
        return inquirytCode.toString();
    }


    /**
     * 物流单编码
     * 生成规则:WL+6位年月日+4位随机数+4位流水号
     *
     * @author 赵盼飞
     */
    public String createLogisticsCode() {
        StringBuilder logisticsCode = new StringBuilder("WL");
        //6位年月日
        logisticsCode.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        logisticsCode.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STOREALLOT_ALLOT_CODE);
        logisticsCode.append(lastStr);
        return logisticsCode.toString();
    }

    /**
     * 付款单编码
     * 生成规则:FK+6位年月日+4位随机数+4位流水号
     *
     * @author: 赵盼飞
     */
    public String createFinPurchCode() {
        StringBuilder logisticsCode = new StringBuilder("FK");
        //6位年月日
        logisticsCode.append(this.getDateStr());
        //获得四位随机数
        int randNumber = this.getRandom(1000, 10000);
        logisticsCode.append(randNumber);
        //获得4位增长数
        String lastStr = this.getLastNum(XeVenSeqContents.ERP_STOREALLOT_ALLOT_CODE);
        logisticsCode.append(lastStr);
        return logisticsCode.toString();
    }


}
